<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Tag_Models_Mysql_TagItem extends Modules_Tag_Models_TagItem
{
	public function delete($item)
	{
		$sql = sprintf("DELETE FROM " . $this->_conn->_tablePrefix . "tag_item_assoc
						WHERE item_id = '%s' AND item_name = '%s' AND route_name = '%s' AND details_route_name = '%s'",
						$this->_conn->escape($item['item_id']),
						$this->_conn->escape($item['item_name']),
						$this->_conn->escape($item['route_name']),
						$this->_conn->escape($item['details_route_name']));
		$this->_conn->query($sql);
		return $this->_conn->affectedRows();	
	}
	
	public function add($item)
	{
		$sql = sprintf("INSERT INTO " . $this->_conn->_tablePrefix . "tag_item_assoc (tag_id, item_id, item_name, route_name, details_route_name, params)
						VALUES ('%s', '%s', '%s', '%s', '%s', '%s')",
						$this->_conn->escape($item['tag_id']),
						$this->_conn->escape($item['item_id']),
						$this->_conn->escape($item['item_name']),
						$this->_conn->escape($item['route_name']),
						$this->_conn->escape($item['details_route_name']),
						$this->_conn->escape($item['item_name'] . ':' . $item['item_id']));
		$this->_conn->query($sql);
	}
	
	public function count($condition = array())
	{
		$sql = 'SELECT COUNT(*) AS num_items FROM ' . $this->_conn->_tablePrefix . 'tag_item_assoc AS t';
		
		if (!empty($condition)) {
			$sql .= ' WHERE 1';
		}
		
		if (isset($condition['tag_id']) && $condition['tag_id']) {
			$sql .= ' AND t.tag_id = "' . $this->_conn->escape($condition['tag_id']) . '"';
		}
		
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_items'];
	}
	
//	public function getTagCloud($routeName, $limit = null)
//	{
//		$sql = sprintf("SELECT ti.details_route_name, t.tag_id, t.tag_text, COUNT(*) AS num_items
//						FROM " . $this->_prefix . "tag_item_assoc AS ti
//						INNER JOIN " . $this->_prefix . "tag AS t
//							ON ti.tag_id = t.tag_id
//						WHERE ti.route_name = '%s'
//						GROUP BY tag_text",
//						mysql_real_escape_string($routeName));
//		if (is_numeric($limit)) {
//			$sql .= sprintf(" LIMIT %s", $limit);		
//		}
//		
//		$rs   = mysql_query($sql);
//		$rows = array();
//		while ($row = mysql_fetch_object($rs)) {
//			$rows[] = $row;
//		}
//		mysql_free_result($rs);
//		return new Tomato_Model_RecordSet($rows, $this);
//	}
}
